{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "input=[(5,8),(4,2),(9,6),(7,7),(3,9),(11,10)] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "indexer_d={\"1\":-1,\"0\":1}\n",
    "def np(dlist):\n",
    "    max_sum_rl=0\n",
    "    max_dlist=dlist.copy()\n",
    "    size=len(dlist)\n",
    "    for i in range(2**size):\n",
    "        cur_dlist=[]\n",
    "        for i,c in enumerate((\"{0:0%db}\"%size).format(i,size)):\n",
    "            cur_dlist.append(input[i][::indexer_d[c]])\n",
    "        sum_rl=0\n",
    "        for i in range(size-1):\n",
    "            sum_rl+=cur_dlist[i][1]*cur_dlist[i+1][0]\n",
    "        if sum_rl>max_sum_rl:\n",
    "            max_sum_rl=sum_rl\n",
    "            max_dlist=cur_dlist\n",
    "    return max_sum_rl,max_dlist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "bin?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(227, [(5, 8), (4, 2), (6, 9), (7, 7), (3, 9), (11, 10)])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np(input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 2)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(1,2)[::1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:math]",
   "language": "python",
   "name": "conda-env-math-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
